package lesson4.api.helpers;

import lesson4.api.ApplicationManager;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WebElementHelper
{
	// Logger
	private static Logger log = LoggerFactory.getLogger(WebElementHelper.class);
	// Application manager instance
	private final ApplicationManager app;

	public WebElementHelper(ApplicationManager p_app)
	{
		app = p_app;
	}

	public void click(By p_locator)
	{
		log.debug("Click on the element: " + p_locator);
		app.driver.findElement(p_locator).click();
	}

	public void type(By p_locator, String p_text)
	{
		log.debug("Type the text in the element: " + p_locator + " => " + p_text);
		app.driver.findElement(p_locator).clear();
		app.driver.findElement(p_locator).sendKeys(p_text);
	}

	public void selectByValue(By p_locator, String p_value)
	{
		log.debug("Select value in the combobox: " + p_locator + " => " + p_value);
		WebElement selectLanguage = app.driver.findElement(p_locator);
		Select select = new Select(selectLanguage);
		select.selectByValue(p_value);
	}

	public boolean isElementPresent(By p_locator)
	{
		try
		{
			if (!app.driver.getCurrentUrl().contains("about:blank"))
			{
				app.driver.findElement(p_locator);
				log.debug("Check for is element present: " + p_locator + " => " + true);
				return true;
			}

			return false;
		}
		catch (NoSuchElementException ex)
		{
			log.debug("Check for is element present: " + p_locator + " => " + false);
			return false;
		}

	}
}
